$(function () {

    $(".i-checks").iCheck({checkboxClass: "icheckbox_square-green-login"});
    $('.imgcode').click(function () {
        var url = "/captcha/captchaImage?type=" + "math" + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });

    var corpId = $("#corpId").val();
    if(corpId && corpId !='' && dd.env.platform!=="notInDingTalk"){
        dd.ready(function () {
            dd.runtime.permission.requestAuthCode({
                corpId: corpId,
                onSuccess: function (result) {
                    var code = result.code;
                    //请求我们服务端的登陆地址
                    $.ajax({
                        url: "/dingtalk/login?authCode=" + code,
                        method: "POST",
                        success: function (result) {
                            console.log(result);
                            var userId = result.result.userid;
                            //location.href = '/index';
                            console.log(userId);
                            $.ajax({
                                type: "post",
                                url: "/ddlogin",
                                data: {
                                    "userId": userId,
                                },
                                success: function (r) {
                                    console.log(r);
                                    if (r.code == 0) {
                                        location.href = '/index';
                                    } else {
                                        // $.modal.closeLoading();
                                        $('.imgcode').click();
                                        $.modal.msg(r.msg);
                                    }
                                }
                            });
                        },
                        error: function (result) {
                            alert(result.toString());
                        }
                    });
                }
            });

        });
    } else {
        $('#divLogin').show();
    }
})


// 登录
$("#btnSubmit").click(function () {
    login();
});

function keyLogin() {
    if (event.keyCode == 13) {
        login();
    }
}

function login() {
    var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var validateCode = $("input[name='validateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');
    // console.log(username + password + validateCode + rememberMe);
    $.ajax({
        type: "post",
        url: "/login",
        data: {
            "username": username,
            "password": password,
            "validateCode": validateCode,
            "rememberMe": rememberMe
        },
        success: function (r) {
            console.log(r);
            if (r.code == 0) {
                location.href = '/index';
            } else {
                // $.modal.closeLoading();
                $('.imgcode').click();
                $.modal.msg(r.msg);
            }
        }
    });
}




